package com.ruoyi.web.controller.equip;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.SelectEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equip.domain.DeviceInfo;
import com.ruoyi.equip.domain.entity.table.ExportParam;
import com.ruoyi.equip.domain.vo.DeviceInfoVO;
import com.ruoyi.equip.domain.vo.TagVo;
import com.ruoyi.equip.service.IDeviceInfoService;
import com.ruoyi.equip.service.ITagInfoService;
import com.ruoyi.td.entity.DevicePropertyLogQueryBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/equip/tag")
@Api(tags = "标签相关接口")
public class EquipTagController extends BaseController {

    @Autowired
    private ITagInfoService tagInfoService;
    @ApiOperation("查询所有标签")
    @GetMapping("/list")
    public  TableDataInfo list(TagVo tagVo)
    {
        startPage();
        List<TagVo> tagInfos =
                tagInfoService.selectTagInfoList(tagVo);
        return getDataTable(tagInfos);
    }

    @ApiOperation("分页查询实时数据")
    @GetMapping("/realData")
    public  TableDataInfo realData(TagVo tagVo)
    {
        startPage();
        List<TagVo> tagInfos =
                tagInfoService.realData(tagVo);
        return getDataTable(tagInfos);
    }



    @ApiOperation("新增标签")
    @Log(title = "新增标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TagVo tagVo)
    {

        tagVo.setCreateBy(getUsername());
        tagVo.setCreateTime(new Date());
        return toAjax(tagInfoService.add(tagVo));
    }


    @ApiOperation("修改指定标签")
    @Log(title = "修改指定标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TagVo tagVo){
        tagVo.setUpdateBy(getUsername());
        return toAjax(tagInfoService.update(tagVo));
    }



    @ApiOperation("删除指定标签")
    @Log(title = "删除指定标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagId}")
    public AjaxResult remove(@PathVariable String tagId) {
        return toAjax(tagInfoService.deleteById(tagId));
    }



    @ApiOperation( "批量删除标签")
    @Log(title = "批量删除标签", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelTag")
    public void batchDelTag( @RequestBody ExportParam exportParam) {
        tagInfoService.batchDelTag(exportParam.getTagIds());
    }


    @ApiOperation("获取指定标签物模型")
    @GetMapping("/getThingModelOfId/{tagId}")
    public AjaxResult getThingModelOfId(@PathVariable String tagId){
        return success(tagInfoService.getThingModelOfId(tagId));
    }


    @ApiOperation("获取指定标签详情")
    @GetMapping("/getTagsDetail/{id}")
    public AjaxResult getTagsDetail(@PathVariable String id){
        return success(tagInfoService.getTagsDetail(id));
    }

    @ApiOperation("标签导出")
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExportParam exportParam) {
        tagInfoService.export(response,exportParam);
    }


    @ApiOperation("解绑指定标签")
    @GetMapping("/unbindDevice/{id}")
    public void unbindDevice(@PathVariable String id){
         tagInfoService.unbindDevice(id);
    }

    @ApiOperation("获取标签属性历史数据")
    @PostMapping("/tagProperty/log/list")
    public  AjaxResult  getPropertyHistory(@Validated @RequestBody
                                           DevicePropertyLogQueryBo logQueryBo) {
        return success(tagInfoService.getPropertyHistory(logQueryBo));
    }
    @ApiOperation("查询所有标签下拉框")
    @GetMapping("/selectList")
    public AjaxResult selectList() {
        List<SelectEntity> selectList = tagInfoService.selectList();
        return success(selectList);
    }
    @ApiOperation("查询所有标签下拉框")
    @GetMapping("/selectList/{deviceId}")
    public AjaxResult selectList(@PathVariable String deviceId) {
        List<SelectEntity> selectList = tagInfoService.selectList(deviceId);
        return success(selectList);
    }

}
